package com.vitabuybuy.tv.widget

import android.annotation.TargetApi
import android.content.Context
import android.content.res.Configuration
import android.net.http.SslError
import android.os.Build
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.webkit.*
import android.widget.ProgressBar
import com.vitabuybuy.tv.R


/**
 * 带进度条的WebView
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
class ProgressWebView : WebView {
    private var progressbar: ProgressBar? = null
    private lateinit var mContext : Context
    constructor(context: Context) : super(if (Build.VERSION.SDK_INT in 21..22) context.createConfigurationContext(Configuration()) else context, null) {
        this.mContext = context
        init()
    }
    constructor(context: Context, attrs: AttributeSet?) : super(if (Build.VERSION.SDK_INT in 21..22) context.createConfigurationContext(Configuration()) else context, attrs) {
        this.mContext = context
        init()
    }
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(if (Build.VERSION.SDK_INT in 21..22) context.createConfigurationContext(Configuration()) else context, attrs, defStyleAttr) {
        this.mContext = context
        init()
    }
    fun init() {
        val view = LayoutInflater.from(mContext).inflate( R.layout.progress_webview_layout, null)
        progressbar = view.findViewById(R.id.progressbar) as ProgressBar
        progressbar!!.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0, 0)
        addView(progressbar)
        webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return true
            }

            override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
                if (error.primaryError == SslError.SSL_INVALID) {// 校验过程遇到了bug
                    handler.proceed()
                } else {
                    handler.cancel()
                }
            }
        }
        webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView, newProgress: Int) {
                if (newProgress == 100) {
                    progressbar!!.visibility = View.GONE
                } else {
                    if (progressbar!!.visibility == View.GONE) {
                        progressbar!!.visibility = View.VISIBLE
                    }
                    progressbar!!.progress = newProgress
                }
                super.onProgressChanged(view, newProgress)
            }
        }
    }

    override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
        val lp = progressbar!!.layoutParams as LayoutParams
        lp.x = l
        lp.y = t
        progressbar!!.layoutParams = lp
        super.onScrollChanged(l, t, oldl, oldt)
    }

    /**
     * 通用设置
     */
    fun defaultSetting() {
        val settings = settings
        //设置WebView属性，能够执行Javascript脚本
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.javaScriptEnabled = true//启用js
//        settings.useWideViewPort = true
//        settings.loadWithOverviewMode = true
        settings.defaultTextEncodingName = "utf-8"
        settings.allowFileAccess = true
        settings.domStorageEnabled = true
        settings.setSupportMultipleWindows(true)
        settings.blockNetworkImage = false//解决图片不显示
        settings.setSupportZoom(false)
        settings.builtInZoomControls = false
        settings.allowFileAccess = true
        settings.databaseEnabled = true
        settings.setGeolocationEnabled(true)
        settings.setAppCacheEnabled(true)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
        }
    }
}
